package client.role;

import client.common.result.Result;
import client.common.vo.RoleVO;
import remote.service.PermissionService;
import remote.service.RoleService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class RoleList extends JFrame {
    private JPanel panel;
    private RoleService roleService;
    private PermissionService permissionService;
    private JButton addRoleButton;
    
    public RoleList() {
        // 更改 UI 风格为 Nimbus，如果 Nimbus 不可用，则使用系统默认风格
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println("Nimbus style is not available, using default style.");
        }
        
        roleService = new RoleService() {
        };
        permissionService = new PermissionService() {
        };
        
        setTitle("角色列表");
        setSize(900, 600);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        
        setLayout(new BorderLayout(10, 10));
        panel = new JPanel(new GridLayout(0, 1, 10, 10));
        add(new JScrollPane(panel), BorderLayout.CENTER);
        
        loadRoles();
        
        JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        addRoleButton = new JButton("添加角色");
        bottomPanel.add(addRoleButton);
        add(bottomPanel, BorderLayout.SOUTH);
        
        addRoleButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AddRoleForm(RoleList.this).setVisible(true);
            }
        });
        
        
    }
    
    public static void main(String[] args) {
        new RoleList();
    }
    
    public void refreshRoleList() {
        panel.removeAll();
        loadRoles();
        panel.revalidate();
        panel.repaint();
    }
    
    private void loadRoles() {
        Result<List<RoleVO>> roleResult = roleService.getRoleList();
        
        if (roleResult.getCode() == 200) {
            this.setVisible(true);
            List<RoleVO> roleList = roleResult.getData();
            
            for (RoleVO role : roleList) {
                JPanel buttonPanel = new JPanel();
                buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
                
                JLabel roleLabel = new JLabel(role.getName());
                roleLabel.setFont(new Font("Serif", Font.BOLD, 30));
                
                int roleId = role.getId();
                
                JButton viewMembersButton = new JButton("查看人员");
                viewMembersButton.setFont(new Font("Serif", Font.PLAIN, 20));
                viewMembersButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        new UserList(roleId, null);
                    }
                });
                
                JLabel roleDescription = new JLabel("(" + role.getDescription() + ")");
                roleDescription.setFont(new Font("Serif", Font.BOLD, 30));
                
                JButton detailsButton = new JButton("查看权限详情");
                detailsButton.setFont(new Font("Serif", Font.PLAIN, 20));
                detailsButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        new RoleDetailDialog(roleId).setVisible(true);
                    }
                });
                
                JButton deleteButton = new JButton("删除角色");
                deleteButton.setFont(new Font("Serif", Font.PLAIN, 20));
                deleteButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int confirm = JOptionPane.showConfirmDialog(RoleList.this,
                                "确认删除角色: " + role.getName() + "?", "确认删除",
                                JOptionPane.YES_NO_OPTION);
                        if (confirm == JOptionPane.YES_OPTION) {
                            Result<String> deleteResult = roleService.deleteRole(roleId);
                            if (deleteResult.getCode() == 200) {
                                JOptionPane.showMessageDialog(RoleList.this, "删除角色成功！");
                                refreshRoleList();
                            } else {
                                JOptionPane.showMessageDialog(RoleList.this, "删除角色失败: " + deleteResult.getMessage());
                            }
                        }
                    }
                });
                
                buttonPanel.add(roleLabel);
                buttonPanel.add(Box.createHorizontalStrut(10));
                buttonPanel.add(roleDescription);
                buttonPanel.add(Box.createHorizontalGlue());
                buttonPanel.add(viewMembersButton);
                buttonPanel.add(Box.createHorizontalStrut(10));
                buttonPanel.add(detailsButton);
                buttonPanel.add(Box.createHorizontalStrut(10));
                buttonPanel.add(deleteButton);
                
                panel.add(buttonPanel);
            }
        } else if (roleResult.getCode() == 403) {
            JOptionPane.showMessageDialog(this, roleResult.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            setVisible(false);
            dispose(); // 关闭窗口
        } else {
            this.setVisible(true);
            JOptionPane.showMessageDialog(this, "获取角色列表失败: " + roleResult.getMessage());
        }
    }
}
